import { reactive, ref } from 'vue'

export default function (listApi) {
	const list = reactive([])
	const search = reactive({})

	const page = ref(1)
	const limit = ref(10)
	const loadStatus = ref('default')

	async function pushList() {
		loadStatus.value = 'loading'
		var params = {
			current: page.value++,
			size: limit.value,
			...search
		}
		const { data } = await listApi(params)
		list.push(...data.records)
		loadStatus.value = data.totalRow > list.length ? 'default' : 'nomore'
	}

	async function refreshList() {
		var params = {
			current: 1,
			size: limit.value,
			...search
		}
		const { data } = await listApi(params)
		page.value = 2
		list.length = 0
		list.push(...data.records)
		loadStatus.value = data.totalRow > list.length ? 'default' : 'nomore'
	}

	async function reloadList() {
		page.value = 1
		list.length = 0
		await pushList()
	}
	return {
		list,
		pushList,
		page,
		limit,
		loadStatus,
		search,
		refreshList,
		reloadList
	}
}
